package com.code.generator;


import com.code.generator.AbstractGenerator;
import com.code.generator.engine.FreemarkerEngine;
import com.code.generator.model.GeneratorConfiguration;
import com.code.generator.model.TableInfo;
import com.code.util.TransformUtils;
import freemarker.template.Configuration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

public class DaoGenerator extends AbstractGenerator {
    public DaoGenerator() {
    }

    public void generate(GeneratorConfiguration daoConfiguration) throws Exception {
        this.saveUserCustomConfig(daoConfiguration);
        List tableInfoList = daoConfiguration.getSelectTableInfoList();
        if(tableInfoList != null && tableInfoList.size() > 0) {
            Iterator var4 = tableInfoList.iterator();

            while(var4.hasNext()) {
                TableInfo tableInfo = (TableInfo)var4.next();
                Map root = this.buildRootForTemplete(daoConfiguration, tableInfo);
                //由表名获取类名
                String className = TransformUtils.getTablesNameToClassName(tableInfo.getTableName());
                //freemaker的Configuration
                Configuration configuration = this.buildFreemarkerConfiguration(daoConfiguration);
                if(tableInfo.isGenerateDomain()) {
                    FreemarkerEngine.createFileByFTL(configuration, root, "entityClass.ftl", FilenameUtils.concat(daoConfiguration.getDomainPath(), className + ".java"));
                }

                if(tableInfo.isGenerateSqlMap()) {
                    this.generateSqlMap(daoConfiguration, tableInfo, configuration, root);
                }

                if(tableInfo.isGenerateDao()) {
                    FreemarkerEngine.createFileByFTL(configuration, root, "daoClass.ftl", FilenameUtils.concat(daoConfiguration.getDaoPath(), className + "Dao.java"));
                    FreemarkerEngine.createFileByFTL(configuration, root, "daoImplClass.ftl", FilenameUtils.concat(daoConfiguration.getDaoImplPath(), className + "DaoImpl.java"));
                }

                tableInfo.isNeedSharding();
                if(tableInfo.isGenerateManager()) {
                    FreemarkerEngine.createFileByFTL(configuration, root, "managerClass.ftl", FilenameUtils.concat(daoConfiguration.getManagerPath(), className + "Manager.java"));
                    FreemarkerEngine.createFileByFTL(configuration, root, "managerImplClass.ftl", FilenameUtils.concat(daoConfiguration.getManagerImplPath(), className + "ManagerImpl.java"));
                }

                if(tableInfo.isGenerateService()) {
                    FreemarkerEngine.createFileByFTL(configuration, root, "serviceClass.ftl", FilenameUtils.concat(daoConfiguration.getServicePath(), className + "Service.java"));
                    FreemarkerEngine.createFileByFTL(configuration, root, "serviceImplClass.ftl", FilenameUtils.concat(daoConfiguration.getServiceImplPath(), className + "ServiceImpl.java"));
                }

                if(tableInfo.isGenerateController()) {
                    FreemarkerEngine.createFileByFTL(configuration, root, "controllerClass.ftl", FilenameUtils.concat(daoConfiguration.getControllerPath(), className + "Controller.java"));
                    FreemarkerEngine.createFileByFTL(configuration, root, "page.ftl", FilenameUtils.concat(daoConfiguration.getVmPath(), "biz" + File.separator + className.toLowerCase() + File.separator + "query" + className + "List.vm"));
                }
            }
        }

    }

    private void generateSqlMap(GeneratorConfiguration conf, TableInfo tableInfo, Configuration configuration, Map<String, Object> root) throws IOException {
        String className = TransformUtils.getTablesNameToClassName(tableInfo.getTableName());
        FreemarkerEngine.createFileByFTL(configuration, root, "sqlMapper.ftl", FilenameUtils.concat(conf.getSqlMapperPath(), className + ".xml"));
        String sqlMapConfigFilePath = FilenameUtils.concat(conf.getSqlMapperConfigPath(), "sqlmap-config.xml");
        File sqlMapConfigFile = new File(sqlMapConfigFilePath);
        if(!sqlMapConfigFile.exists()) {
            FileUtils.forceMkdir(sqlMapConfigFile.getParentFile());
            FreemarkerEngine.createFileByFTL(configuration, root, "sqlmap-config.ftl", sqlMapConfigFilePath);
        }

        System.out.println("sqlmap-config.xml generate. [" + sqlMapConfigFilePath + "]");
        String sqlmapTag = "    <sqlMap resource=\"sqlmap/" + className + ".xml\"/>";
        FileInputStream fis = null;
        String content = null;

        label124: {
            try {
                fis = new FileInputStream(sqlMapConfigFile);
                content = IOUtils.toString(fis, conf.getEncoding());
                int fos = content.indexOf("</sqlMapConfig>");
                if(fos < 1) {
                    throw new RuntimeException("sqlmap-config.xml has no </sqlMapConfig> tag. [" + sqlMapConfigFilePath + "]");
                }

                if(!content.replaceAll("\\s", "").contains(sqlmapTag.replaceAll("\\s", ""))) {
                    System.out.println("sqlmap-config.xml write [" + sqlmapTag + "] tag. [" + sqlMapConfigFilePath + "]");
                    content = content.substring(0, fos) + sqlmapTag + IOUtils.LINE_SEPARATOR + content.substring(fos);
                    break label124;
                }

                System.out.println("sqlmap-config.xml has [" + sqlmapTag + "] tag, no repeat write. [" + sqlMapConfigFilePath + "]");
            } catch (Exception var24) {
                var24.printStackTrace();
                throw new RuntimeException("read sqlmap-config.xml error. [" + sqlMapConfigFilePath + "] " + var24.getMessage());
            } finally {
                IOUtils.closeQuietly(fis);
            }

            return;
        }

        FileOutputStream fos1 = null;

        try {
            fos1 = new FileOutputStream(sqlMapConfigFile);
            IOUtils.write(content, fos1, conf.getEncoding());
        } catch (Exception var22) {
            var22.printStackTrace();
            throw new RuntimeException("write sqlmap-config.xml error. [" + sqlMapConfigFilePath + "] " + var22.getMessage());
        } finally {
            IOUtils.closeQuietly(fos1);
        }

    }
}
